//
//  BaseRequestController.m
//  s.
//
//  Created by fanshijian on 2022/8/24.
//  Copyright © 2022 J. All rights reserved.
//

#import "BaseRequestController.h"

@implementation HLControllerConfig

- (instancetype)init{
    if (self = [super init]) {
        _req_params = NSMutableDictionary.dictionary;
        _conv_url = nil;
        _pageNum = 1;
        _pageKey = @"pageNum";
        _pageSize = 10;
        self.req_params[@"pageSize"] = @(_pageSize);
        self.req_params[@"pageNum"] = @(_pageNum);
    }
    return self;
}
- (void)setPageKey:(NSString *)pageKey{
    if (_pageKey != pageKey || [pageKey isEqual:@"page"]) {
        _pageKey = pageKey;
        [self resetPage];
    }
}
- (void)resetPage{
    _pageNum = 1;
    self.req_params[_pageKey] = @1;
    self.req_params[@"pageSize"] = @(self.pageSize);
}
- (void)nextPage{
    NSLog(@"_pageNum1 : %d",_pageNum);
    self.req_params[_pageKey] = @(++_pageNum);
    NSLog(@"self.req_params[_pageKey] >> %@",self.req_params[_pageKey]);
    NSLog(@"_pageNum2 : %d",_pageNum);
}

- (void)setPageSize:(NSInteger)pageSize {
    _pageSize = pageSize;
    self.req_params[@"pageSize"] = @(pageSize);
}

@end


@interface BaseRequestController () {
    BOOL _should_refresh;
}
@property (weak, nonatomic) AFHTTPRequestOperation *opera;
@end

@implementation BaseRequestController
@synthesize cfg = _cfg;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.refresh_notice_names enumerateObjectsUsingBlock:^(id  _Nonnull obj, BOOL * _Nonnull stop) {
        [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(_willRefreshState) name:obj object:nil];
    }];
}
- (void)_willRefreshState{
    _should_refresh = YES;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_should_refresh) {
        _should_refresh = NO;
        [self.cfg resetPage];
        [self startLoading];
    }
}
- (BOOL)can_show_page_loading {
    return YES;
}

- (BOOL)can_show_req_error{
    return YES;
}
- (NSDictionary *)empty_action{return kMsgForward(@"startLoading", WKProxy(self));};

- (HLControllerConfig *)cfg{
    if (!_cfg) {
        _cfg = [[HLControllerConfig alloc] init];
    }
    return _cfg;
}
- (void)setCfg:(HLControllerConfig *)cfg{
    if (_cfg != cfg) {
        _cfg = cfg;
        [self startLoading];
    }
}

- (void)startLoading {
    [self _pre_load:NO];
}

- (void)_pre_load:(BOOL)more{
    [self _load:more];
}

- (void)_load:(BOOL)more{
    if (_opera.isExecuting) return; //正在刷新
    if (self.showNoContent) self.showNoContent = NO;
    if (self.cfg.conv_url) {
//        if (self.can_show_page_loading) self.showPageLoding = YES;
        _opera = request_conv(self.cfg.conv_url, self.cfg.req_params, ^(AFHTTPRequestOperation *operation, id data) {
            [self _finishLoadWithOperation:operation data:data more:more];
        });
    }
}

- (void)_finishLoadWithOperation:(AFHTTPRequestOperation *)operation data:(id)data more:(BOOL)more {
    verifyKlResponse(operation, data, self.can_show_req_error, ^(NSString *msg) {
        if (msg) {
            self.showNoContent = YES;
        }else {
            self.showNoContent = NO;
        }
        if (self.showPageLoding) self.showPageLoding = NO;
        [self finishRequestWithOperation:operation data:data vmsg:msg more:more];
    });
}

- (void)finishRequestWithOperation:(AFHTTPRequestOperation *)operation data:(id)data vmsg:(NSString *)msg more:(BOOL)more {
    
}

- (void)refreshRequest{
    [self.cfg resetPage];
    if ([self willRefresh]) {[self _pre_load:NO];}
}

/** 加载更多  */
- (void)loadMore{
    [self willLoadMore];
    [self _pre_load:YES];
}

- (BOOL)willRefresh{
    return YES;
}

- (void)willLoadMore{
    [self.cfg nextPage];
}


@end
